//
//  ReadListViewController.m
//  Leisure
//
//  Created by xalo on 16/6/29.
//  Copyright © 2016年 张凡. All rights reserved.
//

#import "ReadListViewController.h"

@interface ReadListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UISegmentedControl *segmentedControl;//分段视图控制器
@property (weak, nonatomic) IBOutlet UITableView *newestTableView;//最新列表
@property (weak, nonatomic) IBOutlet UITableView *hotTableView;//最热列表
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;//滚动视图
@property (nonatomic, assign)NSInteger newStart;//最新列表数据的起始位置
@property (nonatomic, assign)NSInteger newLimit;//最新列表数据的条数
@property (nonatomic, assign)NSInteger hotStart;//最热列表数据的起始位置
@property (nonatomic, assign)NSInteger hotLimit;//最热列表数据的条数
@property (nonatomic, strong)NSMutableArray *newestDataArray;//最新列表数据源
@property (nonatomic, strong)NSMutableArray *hotDataArray;//最热列表数据源

@end

@implementation ReadListViewController

- (NSMutableArray *)newestDataArray{
    
    if (!_newestDataArray) {
        _newestDataArray = [NSMutableArray array];
    }
    return _newestDataArray;
}

- (NSMutableArray *)hotDataArray{
    
    if (!_hotDataArray) {
        _hotDataArray = [NSMutableArray array];
    }
    return _hotDataArray;
}

- (UISegmentedControl *)segmentedControl{
    
    if (!_segmentedControl) {
        _segmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
        //插入标签
        [_segmentedControl insertSegmentWithTitle:@"最新" atIndex:0 animated:YES];
        [_segmentedControl insertSegmentWithTitle:@"最热" atIndex:0 animated:YES];
        [_segmentedControl addTarget:self action:@selector(clickSegmentedControl:) forControlEvents:UIControlEventValueChanged];
    }
    return _segmentedControl;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.backgroundColor = [UIColor purpleColor];
    self.navigationItem.titleView = self.segmentedControl;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.newestTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.hotTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    //数据请求
    [self loadNewestTableViewDataSourceWithStart:0 limit:20];
    [self loadHotTableViewDataSourceWithStart:0 limit:20];
    
#pragma mark ------最新上拉加载,下拉刷新
    //添加上拉加载
    self.newestTableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        
        //每次增加10
        self.newStart += 10;
        [self loadNewestTableViewDataSourceWithStart:self.newStart limit:20];
    }];
    //添加下拉刷新
    self.newestTableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        
        //刷新之前清空数组
        [self.newestDataArray removeAllObjects];
        [self loadNewestTableViewDataSourceWithStart:0 limit:20];
    }];
    
#pragma mark -----最热上拉加载,下拉刷新
    //添加上拉加载
    self.hotTableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        
        self.hotStart += 10;
        [self loadHotTableViewDataSourceWithStart:self.hotStart limit:20];
    }];
    //添加下拉刷新
    self.hotTableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        
        [self.hotDataArray removeAllObjects];
        [self loadHotTableViewDataSourceWithStart:self.hotStart limit:20];
    }];
    
    //注册cell
//    [self.newestTableView registerClass:[BaseCell class]  forCellReuseIdentifier:@"ReadListModel_cell"];
//    [self.hotTableView registerClass:[BaseCell class]  forCellReuseIdentifier:@"ReadListModel_cell"];
    [self.newestTableView registerNib:[UINib nibWithNibName:@"ReadListCell" bundle:nil] forCellReuseIdentifier:@"ReadListModel_cell"];
    [self.hotTableView registerNib:[UINib nibWithNibName:@"ReadListCell" bundle:nil] forCellReuseIdentifier:@"ReadListModel_cell"];
    
}

//分段视图控制器的点击事件
- (void)clickSegmentedControl:(UISegmentedControl *)sender{
    
    if (sender.selectedSegmentIndex == 0) {
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }else{
        
        [self.scrollView setContentOffset:CGPointMake(kWidth, 0) animated:YES];
    }
}

#pragma mark ----数据请求
//加载最新列表数据
- (void)loadNewestTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit{
    
    [RequsetManager fetchDataWithUrl:kReadListUrl parametr:@{@"typeid":@(self.typeID), @"start":@(start), @"limit":@(limit), @"sort":@"addtime"} mehtod:@"POST" complete:^(NSData *data) {
        
        NSDictionary *bigDic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            
            ReadListModel *model = [[ReadListModel alloc] init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.newestDataArray addObject:model];
        }
    } updateUI:^{
        
        //结束刷新
        [self.newestTableView.mj_header endRefreshing];
        [self.newestTableView.mj_footer endRefreshing];
        [self.newestTableView reloadData];
    }];
}
//加载最热列表数据
- (void)loadHotTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit{
    
    [RequsetManager fetchDataWithUrl:kReadListUrl parametr:@{@"typeid":@(self.typeID), @"start":@(start), @"limit":@(limit), @"sort":@"hot"} mehtod:@"POST" complete:^(NSData *data) {
    
        NSDictionary *bigDic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            
            ReadListModel *model = [[ReadListModel alloc] init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.hotDataArray addObject:model];
        }
        
    } updateUI:^{
        
        [self.hotTableView.mj_header endRefreshing];
        [self.hotTableView.mj_footer endRefreshing];
        [self.hotTableView reloadData];
    }];
}

#pragma mark ---UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //判断滚动的是不是你要滚的
    if (scrollView ==self.scrollView) {
        if (self.scrollView.contentOffset.x == 0) {
            self.segmentedControl.selectedSegmentIndex = 0;
        }else if (self.scrollView.contentOffset.x == kWidth){
            self.segmentedControl.selectedSegmentIndex = 1;
        }
    }
}

#pragma mark -------UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView == self.newestTableView) {
        return self.newestDataArray.count;
    }else{
        return self.hotDataArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    //创建一个model来接收
    ReadListModel *model = [[ReadListModel alloc] init];
    if (tableView == self.newestTableView) {
        
        model = self.newestDataArray[indexPath.row];
    }else{
        model = self.hotDataArray[indexPath.row];
    }
    
    //工厂模式产出来的cell
    BaseCell *cell = [FactoryMode productCellWithModel:model tableView:tableView indexPath:indexPath];
    cell.selectedBackgroundView = [UIView new];
    
//    cell.textLabel.text = model.title;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ReadListInfoViewController *readListInfo = [[ReadListInfoViewController alloc] init];
    if (tableView == self.newestTableView) {
        readListInfo.contentID = [self.newestDataArray[indexPath.row] ID];
    }else{
        readListInfo.contentID = [self.hotDataArray[indexPath.row] ID];
    }
    
    [self.navigationController pushViewController:readListInfo animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
